package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.PlanProgressVO;
import com.yonyou.pmclouds.plan.entity.ProjectPlanVO;
import com.yonyou.pmclouds.plan.mapper.PlanProgressMapper;

public class PlanInsertProgressCheck implements SingleProcessor<ProjectPlanVO> {
    private ProjectPlanVO parent;
    private PlanProgressMapper mapper;

    public PlanInsertProgressCheck(ProjectPlanVO parent, PlanProgressMapper mapper) {
        this.parent = parent;
        this.mapper = mapper;
    }

    @Override
    public void process(ProjectPlanVO vo, ProjectPlanVO orig) throws BusinessException {
        // 不存在上级或者上级已经存在其他子节点，就不需要核对进度了
        if (parent == null || parent.getChildCount() > 1) {
            return;
        }
        String parentId = parent.getPkPlan();
        PlanProgressVO prog = mapper.getById(parentId);
        if (prog != null && prog.getProgress() != 0) {
            throw new BusinessException("当前计划项的进度反馈已大于0%，不允许增加下级");
        }
    }
}
